<template>
  <div v-loading.lock="loading" class="base-content">
    <div class="basic-info">
      <el-form ref="queryForm" :inline="true" :model="queryForm">
        <el-form-item label="任务编号" prop="EntrustTaskNum">
          <el-input v-model="queryForm.EntrustTaskNum" placeholder="任务编号" />
        </el-form-item>
        <el-form-item class="base-filter-button">
          <el-button type="primary" @click="onQuery">查询</el-button>
          <el-button @click="cancelForm">清空</el-button>
        </el-form-item>
      </el-form>
      <div class="base-content-operate">
        <el-button
          :disabled="!submitLoad"
          type="primary"
          icon="el-icon-plus"
          @click="entrustTaskReceiveUpdateFun"
          >接收</el-button
        >
        <el-button
          type="primary"
          icon="el-icon-plus"
          @click="entrustTaskDistributionFun"
          >转单</el-button
        >
        <!-- <el-button type="primary" icon="el-icon-plus" @click="search"
          >查询</el-button
        >-->
        <!-- <el-button
          icon="el-icon-document-copy"
          type="primary"
          @click="exprotData"
          >导出</el-button
        >-->
      </div>
      <el-table
        :data="list"
        element-loading-text="Loading"
        tooltip-effect="dark"
        height="calc(100% - 150px)"
        @selection-change="handleSelectionChange"
      >
        <el-table-column type="selection" width="55"></el-table-column>
        <el-table-column align="center" label="序号" width="50">
          <template slot-scope="scope">{{ scope.$index + 1 }}</template>
        </el-table-column>
        <el-table-column label="任务编号">
          <template slot-scope="scope">{{ scope.row.entrustTaskNum }}</template>
        </el-table-column>
        <el-table-column label="委托单号">
          <template slot-scope="scope">{{
            scope.row.entrustOrderNumList
          }}</template>
        </el-table-column>
        <el-table-column label="检测项目">
          <template slot-scope="scope">{{
            scope.row.detectionItemName
          }}</template>
        </el-table-column>
        <el-table-column label="样品名称">
          <template slot-scope="scope">{{ scope.row.sampleInfoName }}</template>
        </el-table-column>
        <el-table-column label="样品编号">
          <template slot-scope="scope">{{ scope.row.sampleInfoNum }}</template>
        </el-table-column>
        <el-table-column label="计划检测时间">
          <template slot-scope="scope">{{
            formatTime(scope.row.planDetectionDate)
          }}</template>
        </el-table-column>
        <el-table-column label="分配人">
          <template slot-scope="scope">{{
            scope.row.createPersonName
          }}</template>
        </el-table-column>
        <el-table-column label="分配时间">
          <template slot-scope="scope">{{
            formatTime(scope.row.updateTime)
          }}</template>
        </el-table-column>
        <el-table-column label="备注">
          <template slot-scope="scope">{{ scope.row.remark }}</template>
        </el-table-column>
      </el-table>
      <BasePagination
        :total="pagination.total"
        :page-index="pagination.pageIndex"
        :page-size="pagination.pageSize"
        @sizeChange="sizeChange"
        @changePage="currentChange"
      />
    </div>
    <el-dialog
      v-if="dialogFormVisible"
      v-loading.lock="loading"
      class="base-dialog"
      :custom-class="'panna-dialog_medium'"
      title="转单"
      align="center"
      :close-on-click-modal="false"
      :visible.sync="dialogFormVisible"
      @closed="cancelForm"
    >
      <el-form
        ref="formData"
        :model="formData"
        status-icon
        :rules="rules"
        class="receivePerson"
      >
        <el-form-item
          label="接收人"
          :label-width="formLabelWidth"
          prop="receivePersonId"
        >
          <el-select v-model="formData.receivePersonId" placeholder="接收人">
            <el-option
              v-for="item in userArrary"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            ></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer" align="center">
        <el-button
          :disabled="!submitLoad"
          type="primary"
          @click="entrustTaskReassignmentUpdateFun()"
          >确 定</el-button
        >
        <el-button @click="cancelForm">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { getUserListNoPage } from "../../../api/Permission/PermissionUser";
import {
  deleteRecord,
  getEntrustTaskReceiveList,
  entrustTaskReceiveUpdate,
  entrustTaskReassignmentUpdate,
} from "../../../api/EntrustTaskManage/EntrustTask.js";
import _ from "lodash";
import { formatTime } from "../../../utils/time";
export default {
  name: "EntrustTaskReceive",
  data() {
    return {
      submitLoad: true,
      formLabelWidth: 150,
      formData: {
        receivePersonId: "",
      },
      rules: {
        receivePersonId: [
          { required: true, message: "请选择接收人", trigger: "blur" },
        ],
      },
      userArrary: [],
      tableDatas: [],
      loading: true,
      list: [],
      queryForm: {
        EntrustTaskNum: "",
        sysBusinessSampleInfoId: 0,
      },
      dialogFormVisible: false,
      pagination: {
        pageIndex: 1,
        pageSize: 10,
        total: 0,
      },
    };
  },
  created() {
    // 初始化数据
    if (this.$route.query.entrustTaskNum !== undefined) {
      this.queryForm.EntrustTaskNum = this.$route.query.entrustTaskNum;
    }

    if (this.$route.query.sysBusinessSampleInfoId) {
      this.queryForm.sysBusinessSampleInfoId =
        this.$route.query.sysBusinessSampleInfoId;
    }
    this.getUserListNoPageFun();
    this.getList("filter");
  },
  methods: {
    formatTime,
    search() {},
    exprotData() {},
    currentChange(val) {
      this.pagination.pageIndex = val;
      this.getList();
    },
    sizeChange(val) {
      this.pagination.pageIndex = 1;
      this.pagination.pageSize = val;
      this.getList("filter");
    },
    getUserListNoPageFun() {
      var pars = {};
      this.userArrary = [];
      getUserListNoPage(pars)
        .then((res) => {
          this.userArrary = res.data;
        })
        .catch((res) => {});
    },
    getList(flag) {
      if (flag) {
        this.pagination.pageIndex = 1;
      }
      this.list = [];
      this.loading = true;
      getEntrustTaskReceiveList(this.queryForm, this.pagination)
        .then((res) => {
          this.list = res.data.dataList ? res.data.dataList : [];
          this.pagination.total = res.data.total;
          this.loading = false;
        })
        .catch(() => {
          this.loading = false;
        });
    },
    // 接收任务
    entrustTaskReceiveUpdateFun: _.throttle(function() {
      if (this.submitLoad) {
        this.submitLoad = false;
        if (this.tableDatas.length <= 0) {
          this.$message.warning("请选择数据");
          this.submitLoad = true;
          return;
        }
        var obj = {};
        obj.IdList = [];
        for (var i = 0; i < this.tableDatas.length; i++) {
          obj.IdList.push(this.tableDatas[i].id);
        }
        entrustTaskReceiveUpdate(obj)
          .then((res) => {
            if (res.code === 200) {
              this.submitLoad = true;
              this.getList("filter");
            }
          })
          .catch((res) => {
            this.$message.warning("操作失败");
            this.submitLoad = true;
          });
      }
    }, 2000),
    // 转派任务
    entrustTaskReassignmentUpdateFun: _.throttle(function() {
      if (this.submitLoad) {
        this.submitLoad = false;
        this.$refs["formData"].validate((valid) => {
          if (valid) {
            var obj = {};
            obj.IdList = [];
            obj.ReceivePersonId = this.formData.receivePersonId;
            for (var i = 0; i < this.tableDatas.length; i++) {
              obj.IdList.push(this.tableDatas[i].id);
            }
            entrustTaskReassignmentUpdate(obj)
              .then((res) => {
                if (res.code === 200) {
                  this.$message.success("转单成功");
                  this.dialogFormVisible = false;
                  this.submitLoad = true;
                  this.getList();
                }
              })
              .catch((res) => {
                this.$message.warning("操作失败");
                this.submitLoad = true;
              });
          }
        });
      }
    }, 2000),

    entrustTaskDistributionFun() {
      if (this.tableDatas.length <= 0) {
        this.$message.warning("请选择数据");
        return;
      }
      this.dialogFormVisible = true;
    },
    handleDelete(rowData) {
      this.Helper.deleteConfirm(() => {
        this.loading = true;
        deleteRecord({ id: rowData.id })
          .then((res) => {
            this.getList();
            this.loading = false;
          })
          .catch(() => {
            this.loading = false;
          });
      });
    },

    onQuery() {
      this.getList();
    },
    // 清空筛选条件
    cancelForm() {
      this.$refs.queryForm.resetFields();
      this.dialogFormVisible = false;
      this.getList();
    },
    // 选择列表
    handleSelectionChange(val) {
      this.tableDatas = val;
    },
  },
};
</script>
<style lang="scss">
.receivePerson {
  .el-form-item {
    display: flex;
    flex-direction: row;
  }
  .el-form-item__content {
    width: calc(100% - 160px);
  }
}
</style>
